﻿@using KhoaLuan.Resources.Cart
@{
    ViewBag.Title = "Index";
    var items = KhoaLuan.Models.ShoppingCart.Cart.Items;
}


<h3 style="margin-left: 20px; color: #054D0F;">@Cart.QuanLyGioHang</h3>
@using (Html.BeginForm())
{
    <table class="table table-hover">
        <tr>
            <th>@Cart.Name</th>
            <th>@Cart.DonGia</th>
            <th>@Cart.SoLuong</th>
            <th>@Cart.Sotien</th>
            <th></th>
        </tr>
        @foreach (var p in items)
        {
            <tr>
                <td>@p.Name</td>
                <td>@Html.FormatValue(p.UnitPrice, "{0:c}") </td>
                
                <td>
                    <input name="@p.Id" value="@p.Quantity" type="number"  min="1" max="5" size="3" />
                </td>
                <td>@Html.FormatValue(p.UnitPrice * p.Quantity * (1 - p.Discount), " {0:c}")</td>
                <td>
                    <a class="remove-from-cart" href="">
                        <img src="~/images/icons/Trash.png" />
                    </a>
                </td>
            </tr>
        }
    </table>
    <div>
        <button onclick="form.action='/Cart/Update'" class="btn btn-default">@Cart.CapNhapGioHang</button>
        <button onclick="form.action='/Cart/Clear'" class="btn btn-default">@Cart.Xoahet</button>
        <a class="btn btn-default" href="/Product/Index">@Cart.QuayLaiMuaHang</a>
        @if (!Request.IsAuthenticated)
        {
            <a onclick="{ alert('vui lòng đăng nhập'); return false }"
               class="btn btn-default" href="#">@Cart.ThanhToan</a>
        }
        else if (KhoaLuan.Models.ShoppingCart.Cart.Count == 0)
        {
            <a onclick="{ alert('Vui lòng chịn hàng hóa để mua'); return false }"
               class="btn btn-default" href="">@Cart.ThanhToan</a>
        }
        else
        {
            <a class="btn btn-default" href="/Order/Checkout">@Cart.ThanhToan</a>
        }

    </div>
}

@section scripts{

    <script src="~/jquery-ui/jquery-ui.min.js"></script>
    <link href="~/jquery-ui/jquery-ui.min.css" rel="stylesheet" />
    <style id="cart-fly"></style>
    <script>
        $(function () {
            $("a.remove-from-cart").click(function () {
                $(this).parents("tr").hide(2000);

                href = $(this).attr("href");
                $.ajax({
                    url: href,
                    data: {},
                    success: function (response) {
                        $("#cart-count").html(response.Count + " items");
                        $("#cart-amount").html("$ " + response.Amount);
                    }
                });
                return false;// hủy chức năng của liên kết
            });
        });
    </script>
}
